<?php

namespace App\Admin\Controllers;

use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'))->sortable();
        $grid->column('nickname', __('昵称'));
        $grid->column('gender', __('性别'))->replace([1=>'男',2=>'女']);
        $grid->column('avatar', __('头像'))->image(null,50,50);
        $grid->column('phone', __('手机号码'));
        $grid->column('address', __('地址'));
        $grid->column('created_at', __('注册时间'));
        $grid->column('updated_at', __('修改时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('nickname', __('昵称'));
        $show->field('gender', __('性别'))->using([1=>'男',2=>'女']);
        $show->field('avatar', __('头像'))->image();
        $show->field('phone', __('手机号码'));
        $show->field('address', __('地址'));
        $show->field('created_at', __('注册时间'));
        $show->field('updated_at', __('修改时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('nickname', __('昵称'));
        $form->radio('gender', __('性别'))->options([1=>'男',2=>'女'])->default('男');
        $form->image('avatar', __('头像'));
        $form->mobile('phone', __('手机号码'));
        $form->textarea('address', __('地址'));

        return $form;
    }
}
